/**
 * 
 */
/*
 *This method is for the creation of collapsibles that show the information of the CURRENT incomplete orders 
 *(definition of incomplete orders is: orders that have not yet happened. So unconfirmed orders and confirmed orders
 *that have not yet been carried out are in "current orders")
 *It takes an array of object literals and uses the information in those literals to create collapsibles of content. 
 *Each collapsible has a 'cancel' button that has a (*NOT YET DEFINED* function to communicate with the server that 
 *the order has been canceled *NOT YET DEFINED*) and removes the order information from the GUI.
	
*/


var page = 0;
var maxPage = 1;
var numSuccess, numWaiting;
var busyIndicator = new WL.BusyIndicator('historicalorder');

(function($){
	
	$("#historicalorder").on('pageshow',function(){	
		WL.Logger.info("historical-orders page show!");
		busyIndicator.show();
		getHisOrders();
				
	});
	
})(jQuery);


function getHisOrders(){
	WL.Logger.info("getHisOrders function ");
	var cus_id = window.customer.id;
	WL.Logger.info("cus_id = " + cus_id);
	page++;
	if (page > maxPage) {
		WL.Logger.info("page = " + page + " max = " + maxPage);
		alert("last page!");
	} else {
		WL.Logger.info("load next page");
		var invocationData = {
				adapter : 'stAdp',
				procedure : 'getHistoricalOrderReq',
				//parameters : [{'cus_id': cus_id, 'page': page}],
				parameters : [{'cus_id': "1", 'page': page}],
			};
		WL.Client.invokeProcedure(invocationData,{
			timeout:20000,
			onSuccess : getHisOrdersSuccess,
			onFailure : getHisOrdersFailure
		});
	}	
}
function getHisOrdersSuccess(result){	
	busyIndicator.hide();
	WL.Logger.info("get History order success! ");
	if(result.invocationResult.status == 0) {
		WL.Logger.info("getHisOrders request success! page = " + page);
		orders = result.invocationResult.order_history;
		maxPage = result.invocationResult.maxPage;
		//successPercent = result.invocationResult.succ_percent;
		WL.Logger.info("order length = " + orders.length);		
		if (orders.length == 0) {
			alert("no orders!");
		} else {
			//$("#infocenter-text").val("success = " + numSuccess + " waiting = " + numWaiting).trigger( "create" );
			if (page == 1)
				listOrders(orders);
			else {
				removeGetMoreBtn();
				listOrders(orders);
			}
		}	
	} else {
		WL.Logger.error("getHisOrders request fail!");
	}	
	
}
function getHisOrdersFailure(result){
	busyIndicator.hide();
	alert("getHisOrders request fail!");
	WL.Logger.error("getHisOrders request fail!");
}

function listOrders(orderlist) {
	WL.Logger.info("list order function ");
	var acount = orderlist.length;
	for(var i = 0; i < acount; i++){
		var orderStateImg = "../images/success.png";
		var orderResult = "UNKNOWN";
		var orderId = orderlist[i]._id;
		//var orderType = orderlist[i].type;
		var orderTime = orderlist[i].time;
		var path = "from:" + orderlist[i].start + " to:" + orderlist[i].end;
		
		var dri_info = orderlist[i].dri_info;
		var photoUrl = dri_info.photo;
		var driverName = dri_info.name;
		var driverPhone = dri_info.phone;
		var driverTaxiNumber = dri_info.taxi_number;
		var driverCredit = dri_info.credit;
		
		if (orderlist[i].status == 6) {
			orderResult = "CUSTOMER ACCEPT";
			//var orderStateImg = "../images/success.png";
			var orderStateImg = "check";
		}
		else if (orderlist[i].status == 4) {
			orderResult = "Waiting";
			//var orderStateImg = "../images/fail.png";
			var orderStateImg = "delete";
		}
	
		$("#historicalOrderPort").append(
				"<div data-role='collapsible' data-theme='a' data-content-theme='d' id='collapsible" + i +"'>" +
				"<h5>Time: " + orderTime + "</h5>" +	
				"<h4>" + orderResult + "</h4><hr />" +
				
				"<table>" +
					"<tr><td><img src='../images/avatar.png'></td>" +
					"<td>" +
						"<a data-role='button' data-icon='star' data-iconpos='notext' data-inline='true'></a>" +
						"<a data-role='button' data-icon='star' data-iconpos='notext' data-inline='true'></a>" +
						"<a data-role='button' data-icon='star' data-iconpos='notext' data-inline='true'></a>" +
					"</td></tr>" +
				"</table><hr />" +
				"<table>" +
					"<tr><td><h5>Name:</h5></td>" +
						"<td><h5>" + driverName + "</h5></td></tr>" +
					"<tr><td><h5>Phone:</h5></td>" +
						"<td><h5>" +driverPhone + "</h5></td></tr>" +
					"<tr><td><h5>Taxi:</h5></td>" +
					"<td><h5>" + driverTaxiNumber + "</h5></td></tr>" +
					"<tr><td><h5>OrderID:</h5></td>" + 
						"<td><h5>" + orderId + "</h5></td></tr>" +
					"<tr><td><h5>Journey:</h5></td>" +
						"<td><p><h5>" + path + "</h5></p></td></tr>" +
					"<tr><td><h5>OrderType:</h5></td>" +
						"<td><h5>" + orderType + "</h5></td></tr>" +
				"</table><hr />" +		
				"<a href='#mark_area' data-role='button' data-rel='popup' id='rateOrderBtn" + i + "' data-icon='star' data-transition='pop'>Rate</a></div>").trigger( "create" );	
	}
	
	addGetMoreBtn();
/* 
	//creates event handlers for each button to delete the collapsible
	for(var i = 0; i < acount; i++){
		$("#cancelOrderBtn" + i).click(function(){
			// add 'cancel order' communication with server here
			$($(this).parents()[1]).remove();
		});
	}*/
}

function addGetMoreBtn() {
	$("#currentOrderPort").append(
		"<a href='#' data-role='button' id='get-more-btn'>load more</a></div>").trigger( "create" );
	$("#get-more-btn").click(function(){
		busyIndicator.show();
		getHisOrders();
	});
}

function removeGetMoreBtn() {
	$("#get-more-btn").remove();
}